/**
 *create by Qiu Yongchun on 2017/12/6
 */
let images = require("images");
const fs = require('fs');
const TextToSVG = require('text-to-svg');
const svg2png = require("svg2png");
const Promise = require('bluebird');
const path = require('path');
const qr = require('qr-image');
const md5 = require('blueimp-md5/js/md5');


function certification(number, name, sTime, eTime, term, _txid, authentiTime, random,callback){
    let font_path = path.join(path.join("./service/certification"),"/aa.ttf");
    let image_path = path.join(path.join("./service/images"));
    let imageName = `${md5(_txid)}${eTime}.png`;

    if(fs.existsSync(path.join(image_path,imageName))){
        callback(fs.readFileSync(path.join(image_path,imageName)));
    }


    Promise.promisifyAll(fs);
    let bianhao = "编号：" + number;
    let contentl1 = name + "于"+sTime+"至"+eTime+",参加苏州同济区块";
    let contentl2 = "链研究院组织区块链人才培养"+term+"课程，完成所要求";
    let contentl3 = "的课程内容学习。";
    let content2 = "  特发此证！";
    let txid = " 区块链标识码:"+_txid;
    let company = "苏州同济区块链研究院";

    const textToSVG = TextToSVG.loadSync(font_path);

    const sourceImg = images(path.join(image_path,"qualifications_bg.jpg"));
    // const sourceImg = images('../images/qualifications_bg.jpg');
    const sWidth = sourceImg.width();
    const sHeight = sourceImg.height();

    /*****************bianhao*/
    const svg2 = textToSVG.getSVG(bianhao, {x: 0, y: 0, fontSize: 24, anchor: 'top',});
    /*****************content*/
    const svg31 = textToSVG.getSVG(contentl1, {x: 0, y: 0, fontSize: 36, anchor: 'top',});
    const svg32 = textToSVG.getSVG(contentl2, {x: 0, y: 0, fontSize: 36, anchor: 'top',});
    const svg33 = textToSVG.getSVG(contentl3, {x: 0, y: 0, fontSize: 36, anchor: 'top',});
    /*****************content2*/
    const svg4 = textToSVG.getSVG(content2, {x: 0, y: 0, fontSize: 36, anchor: 'top',});
    /*****************txid*/
    const svg5 = textToSVG.getSVG(txid, {x: 0, y: 0, fontSize: 20, anchor: 'top',});
    /*****************company*/
    const svg6 = textToSVG.getSVG(company, {x: 0, y: 0, fontSize: 24, anchor: 'top',});
    /*****************authentiTime*/
    const svg7 = textToSVG.getSVG(authentiTime, {x: 0, y: 0, fontSize: 24, anchor: 'top', width:"200"});
    /**************** 二维码 */
    // const qr_PNG = qr.imageSync("http://cert.tj-fintech.com/certs/getCert?txHex="+_txid, {
    const qr_PNG = qr.imageSync("http://cert.tj-fintech.com?fromQr="+_txid, {
        type: 'png',
        size:3,
        margin:1
    });

    Promise.coroutine(function* generateInvitationCard() {
        const targetImg2Path = path.join(image_path,"2.png");
        const targetImg31Path = path.join(image_path,"31.png");
        const targetImg32Path = path.join(image_path,"32.png");
        const targetImg33Path = path.join(image_path,"33.png");
        const targetImg4Path = path.join(image_path,"4.png");
        const targetImg5Path = path.join(image_path,"5.png");
        const targetImg6Path = path.join(image_path,"6.png");
        const targetImg7Path = path.join(image_path,"7.png");
        const targetImgqrPath = path.join(image_path,"qr.png");
        const targetImg8Path = path.join(image_path,"tjFintech.png");

        const [buffer2, buffer31, buffer32,buffer33,buffer4, buffer5, buffer6, buffer7, bufferQr] = yield Promise.all([
            svg2png(svg2),
            svg2png(svg31),
            svg2png(svg32),
            svg2png(svg33),
            svg2png(svg4),
            svg2png(svg5),
            svg2png(svg6),
            svg2png(svg7),
            new Buffer(qr_PNG),
        ]);

        yield Promise.all([
            fs.writeFileAsync(targetImg2Path, buffer2),
            fs.writeFileAsync(targetImg31Path, buffer31),
            fs.writeFileAsync(targetImg32Path, buffer32),
            fs.writeFileAsync(targetImg33Path, buffer33),
            fs.writeFileAsync(targetImg4Path, buffer4),
            fs.writeFileAsync(targetImg5Path, buffer5),
            fs.writeFileAsync(targetImg6Path, buffer6),
            fs.writeFileAsync(targetImg7Path, buffer7),
            fs.writeFileAsync(targetImgqrPath, bufferQr),
        ]);

        const target2Img = images(targetImg2Path);
        const t2Width = target2Img.width();
        const t2Height = target2Img.height();
        const offsetX2 = (sWidth - t2Width) / 2;
        const offsetY2 = 275;

        const target31Img = images(targetImg31Path);
        const t31Width = target31Img.width();
        const t31Height = target31Img.height();
        const offsetX31 = (sWidth - t31Width) / 2 +50;
        const offsetY31 = 325;

        const target32Img = images(targetImg32Path);
        const t32Width = target32Img.width();
        const t32Height = target32Img.height();
        const offsetX32 = (sWidth - t31Width) / 2;
        const offsetY32 = 375;

        const target33Img = images(targetImg33Path);
        const t33Width = target33Img.width();
        const t33Height = target33Img.height();
        const offsetX33 = (sWidth - t31Width) / 2;
        const offsetY33 = 425;

        const target4Img = images(targetImg4Path);
        const t4Width = target4Img.width();
        const t4Height = target4Img.height();
        const offsetX4 = (sWidth - t31Width) / 2;
        const offsetY4 = 475;

        const target5Img = images(targetImg5Path);
        const t5Width = target5Img.width();
        const t5Height = target5Img.height();
        const offsetX5 = (sWidth - t31Width) / 2;
        const offsetY5 = 550;

        const target6Img = images(targetImg6Path);
        const t6Width = target6Img.width();
        const t6Height = target6Img.height();
        const offsetX6 = (sWidth - t6Width) / 2 + 325;
        const offsetY6 = 650;

        const target7Img = images(targetImg7Path);
        const t7Width = target7Img.width();
        const t7Height = target7Img.height();
        const offsetX7 = (sWidth - t7Width) / 2 + 325;
        const offsetY7 = 700;

        const target8Img = images(targetImg8Path);
        const t8Width = target8Img.width();
        const t8Height = target8Img.height();
        const offsetX8 = (sWidth - t7Width) / 2 + 325;
        const offsetY8 = 625;

        const targetQRImg = images(targetImgqrPath);
        const offsetXQR = (sWidth - t7Width) / 2;
        const offsetYQR = 600;

        // let imageName = `${number}a${random}.png`;

        let a = images(sourceImg)
            .draw(target2Img, offsetX2, offsetY2)
            .draw(target31Img, offsetX31, offsetY31)
            .draw(target32Img, offsetX32, offsetY32)
            .draw(target33Img, offsetX33, offsetY33)
            .draw(target4Img, offsetX4, offsetY4)
            .draw(target5Img, offsetX5, offsetY5)
            .draw(target8Img, offsetX8, offsetY8)
            .draw(target6Img, offsetX6, offsetY6)
            .draw(target7Img, offsetX7, offsetY7)
            .draw(targetQRImg, offsetX5, offsetYQR)
            // .save('../public/certifications/'+number+"a"+random+'.png', { quality : 50 });
            .save(path.join(image_path,imageName), { quality : 50 });
            // .encode("png",{operation:50});

        let data = fs.readFileSync(path.join(image_path,imageName));
        if(typeof callback !== "undefined"){
            // callback(new Buffer(data).toString('base64'));//"data:image/jpeg;base64,"
            // callback(path.join(image_path,imageName));//"data:image/jpeg;base64,"
            callback(new Buffer(data,"binary"));//"data:image/jpeg;base64,"
            // callback(a);//"data:image/jpeg;base64,"
        }

        // console.log(number+"a"+random+".png");
    })().catch(e => console.error(e));
}


//function certification(number, name, sTime, eTime, term, txid, authentiTime, random){
exports.certification = certification;
exports.qr = qr;
